package com.shanghai.shanghaielectric.utils;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StringUtil {
	final static int TEN_THOUSAND = 10000;
	final static int HUN_MILLIONH = 100000000;

	public static boolean isEmail(String str) {
		return str.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	}

	/**
	 * 将文件大小单位B转换为对应的B,KB,MB等
	 */
	public static String formatReadableSize(long size) {
		if (size / (1024 * 1024) > 0) {
			float tmpSize = (float) (size) / (1024 * 1024);
			DecimalFormat df = new DecimalFormat("#.00");
			return "" + df.format(tmpSize) + "M";
		} else if (size / 1024 > 0) {
			return "" + (size / 1024) + "K";
		} else {
			return "" + size + "B";
		}
	}

	public static String formatReadableSize(String size) {
		try {
			return formatReadableSize(Long.parseLong(size));
		} catch (Exception e) {
		}
		return "";
	}

	public static String formatReadableCount(long count) {
		try {
			if (count < TEN_THOUSAND) {
				return String.valueOf(count);
			} else {
				if (count > HUN_MILLIONH) {
					return String.valueOf(count / HUN_MILLIONH).concat("亿+");
				} else {
					return String.valueOf(count / TEN_THOUSAND).concat("万+");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String formatTime(long time, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(new Date(time));
	}

	/**
	 * 
	 * 函数名: formatTimeCompareSec<br>
	 * 功能: <br>
	 *
	 * @param time
	 *            秒
	 * @return
	 */
	public static String formatTimeCompareSec(Long time) {
		if (time != null) {
			return formatTimeCompareMs(time * 1000);
		}
		return "";
	}

	/**
	 * 
	 * 函数名: formatTimeCompareSec<br>
	 * 功能: <br>
	 *
	 * @param time
	 *            毫秒
	 * @return
	 */
	public static String formatTimeCompareMs(Long time) {
		if (time != null) {
			try {
				if (time >= System.currentTimeMillis()) {
					return "刚刚";
				}
				Calendar calendar = Calendar.getInstance();
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH) + 1;
				int day = calendar.get(Calendar.DAY_OF_MONTH);
				int hour = calendar.get(Calendar.HOUR_OF_DAY);
				int minute = calendar.get(Calendar.MINUTE);
				calendar.setTime(new Date(time));
				int postYear = calendar.get(Calendar.YEAR);
				int postMonth = calendar.get(Calendar.MONTH) + 1;
				int postDay = calendar.get(Calendar.DAY_OF_MONTH);
				int postHour = calendar.get(Calendar.HOUR_OF_DAY);
				int postMinute = calendar.get(Calendar.MINUTE);
				int yearSpace = year - postYear;
				int monthSpace = month - postMonth;
				int daySpace = day - postDay;
				int hourSpace = hour - postHour;
				int minuteSpace = minute - postMinute;

				String result = "";
				if (yearSpace > 0) {
					// result = yearSpace + "年前";
					result = formatTime(time, "yyyy-MM-dd HH:mm");
				} else if (monthSpace > 0) {
					// result = monthSpace + "个月前";
					result = formatTime(time, "yyyy-MM-dd HH:mm");
				} else if (daySpace > 0) {
					result = daySpace + "天前";
				} else if (hourSpace > 0) {
					result = hourSpace + "小时前";
				} else if (minuteSpace > 0) {
					result = minuteSpace + "分钟前";
				} else if (minuteSpace <= 0) {
					result = "刚刚";
				} else {
					result = formatTime(time, "yyyy-MM-dd HH:mm");
				}
				return result;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "";
	}

	public static String getString(String str, String emptyDefaultValue) {
		return TextUtils.isEmpty(str) ? emptyDefaultValue : str;
	}

	public static boolean isNULL(String value) {
		return TextUtils.isEmpty(value) ? true : value.toLowerCase().trim().equals("null");
	}

	/**
	 * 返回的字符串形式是形如：2013-10-20 20:58
	 * */
	public static String formatTimeInMillis(long timeInMillis) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(timeInMillis);
		Date date = cal.getTime();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
		String fmt = dateFormat.format(date);

		return fmt;
	}
}
